<?php


namespace app\admin\services\task;


use app\admin\model\Distributor;
use app\admin\model\EquIp;
use app\admin\model\FaceRecognitionStaff;
use app\admin\model\PatrolShop;
use app\admin\model\PatrolShopWarning;
use app\admin\model\SystemConfig;
use app\admin\services\redis\RedisService;

class ShopTourService
{

    public static function initialization($data)
    {
        $tour_time_interval = SystemConfig::where('name','tour_time_interval')->value('value');
        $times = $tour_time_interval * 60 ;
        //查出当前所有店铺的考勤信息
        $shops = Distributor::field('a.id as shop_id,a.check_work_id,b.*')
            ->alias('a')
            ->leftJoin('lg_check_work_attendance b','a.check_work_id = b.id')
            ->where('a.status',1)
            ->where('b.status',2)
            ->whereNotNull('check_work_id')
            ->select();
        foreach ($shops as $key => $value){
            //验证设备是否正常运行
            $check_equip = EquIp::check_shop_equip($value['shop_id']) ;
            if(!$check_equip){
                continue;
            }
            self::check_tour_time($value,$times);
        }
        return true;
    }

    //获取json化的考勤数据
    public static function get_time_array($data,$times)
    {
        $redis = new RedisService();
        $redis_key = 'shop_tour_shop_'.$data['shop_id'].'_'.$data['id'].'_'.date('Ymd');
        $get_cache_key = $redis::get($redis_key);
        if($get_cache_key){
            return $get_cache_key;
        }
        $time_array = [] ;
        $go_time = strtotime($data['go_work_time']);
        $off_time = strtotime($data['off_work_time']);
        $count = ($off_time - $go_time) / $times ;
        $count = 10 ;
        $time = $go_time ;
        for ($i = 1; $i<= $count ; $i++){
            if($i > 1){
                $time = $time + $times ;
            }
            $time_array[$i] = $time ;
        }
        $time_json = json_encode($time_array);
        $redis::set($redis_key,$time_json,86400);
        return $time_json;
    }

    public static function check_tour_time($data,$times)
    {
        $redis = new RedisService();
        $go_time = strtotime($data['go_work_time']);
        $off_time = strtotime($data['off_work_time']);
        //不在上班时间不巡店
        if (time() < $go_time + $times || time() > $off_time){
            return true;
        }
        $redis_key = 'shop_tour_shop_'.$data['shop_id'].'_'.$data['id'].'_'.date('Ymd');
        //获取到最后一次巡店记录
        $time_cache = $redis::get($redis_key);
        if($time_cache){
            //如果有巡店记录则代表店员在柜台
            $old_time = $time_cache + $times ;
            $where[] = ['enter_time','between',[$time_cache,$old_time]];
        }else{
            //没有巡店记录则代表店员可能不在柜台
            $old_time = time() - $times ;
            $where[] = ['enter_time','between',[$old_time,time()]];
        }
        $where[] = ['shop_id','=',$data['shop_id']];
//        print_r($where);ldie;
        $find_face = FaceRecognitionStaff::where($where)->order('id','desc')->find();
//        $find_face = FaceRecognitionStaff::where($where)->order('id','desc')->fetchSql(true)->find();
//        print_r($find_face);die;
        //有店员记录则进行记录，没有需要取预警
        if($find_face){
            $check_where = array(
                'staff_id' => $find_face['id']
            );
            $check_staff = PatrolShop::where($check_where)->find();
            if(!$check_staff){
                $add_data = array(
                    'shop_id' => $find_face['shop_id'] ,
                    'shop_name' => '' ,
                    'shop_encrypt' => '' ,
                    'equip_id' => $find_face['equipment_id'] ,
                    'equip_name' => '' ,
                    'staff_id' => $find_face['id'] ,
                    'channel_id' => $find_face['channel_id'] ,
                    'channel_name' => '' ,
                    'shop_clerks_id' => $find_face['shop_clerks_id'] ,
                    'shop_clerks_name' => '' ,
                    'inspection_picture' => $find_face['photo'] ,
                    'inspection_time' => time() ,
                    'remarks' => '定时巡店'.date('Y-m-d H:i:s',$find_face['enter_time']) ,
                );
                $result = PatrolShop::create($add_data);
                if($result){
                    $redis::set($redis_key,$find_face['enter_time'],$times);
                }
            }
            return true;
        }else{
            //判断是否已经发了预警消息
            $is_send_time = SystemConfig::where('name','early_warn_time_interval')->value('value');
            $wait_time = 1800 ;
            if($is_send_time){
                $wait_time = $is_send_time * 60 ;
            }
            $check_warning = PatrolShopWarning::field('id,shop_id,create_time')->where('shop_id',$data['shop_id'])->order('id desc')->find();
            $end_time = strtotime($check_warning['create_time']) + $wait_time;
            if(time() < $end_time){
                return false;
            }
            $add_data = array(
                'shop_id' => $data['shop_id'] ,
                'shop_name' => '' ,
                'shop_encrypt' => '' ,
                'equip_id' => '' ,
                'equip_name' => '' ,
                'channel_id' => '' ,
                'channel_name' => '' ,
                'shop_clerks_id' => '' ,
                'shop_clerks_name' => '' ,
                'inspection_picture' => '' ,
                'inspection_time' => time() ,
                'status' => 2 ,
                'remarks' => '定时巡店时为发现员工/'.date('Y-m-d H:i:s',time()) ,
            );
            PatrolShopWarning::create($add_data);
        }
        return true;
    }

}